package com.sherlochao.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.sherlochao.dao.SharePhotoDao;
import com.sherlochao.model.SharePhoto;



@Repository("sharePhotoDao")
public class SharePhotoDaoImpl implements SharePhotoDao{
	
	@Resource
	private SessionFactory sessionFactory;
	
	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public SharePhoto get(String id) {
		String hql = "from SharePhoto where id = ?";
		Query query = this.getSession().createQuery(hql);
		query.setString(0, id);
		return (SharePhoto)query.uniqueResult();
	}

	@Override
	public String save(SharePhoto entity) {
		return (String)this.getSession().save(entity);
	}

	@Override
	public void delete(String id) {
		SharePhoto entity = this.load(id);
		this.getSession().delete(entity);
	}

	@Override
	public SharePhoto load(String id) {
		return (SharePhoto)this.getSession().load(SharePhoto.class,id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SharePhoto> getSharePhotoByShareTalkId(String shareTalkId) {
		String hql = "from SharePhoto where shareTalk_id = ?";
		Query query = this.getSession().createQuery(hql);
		query.setString(0, shareTalkId);
		List<SharePhoto> sharePhoto = query.list();
		return sharePhoto;
	}
}
